<?php

namespace App\Logic\Member\Form;

use App\Core\BaseListForm;
use App\Core\Exception\DefaultException;
use App\Model\Member\MemberProfitScoreModel;
use App\Model\UserModel;
use App\Util\CommonHelper;
use App\Util\UserIdentityHelper;

class ProfitScoreGroupListForm extends BaseListForm
{
    public $id;
    public $user_id;
    public $score;
    public $before_score;
    public $after_score;
    public $remark;
    public $type;
    public $object_id;
    public $created_at;
    public $start_create_at;
    public $end_create_at;
    public $group;
    public $date;


    /**
     * 收益账户积分变动列表
     * @return \App\Core\Output
     * @throws DefaultException
     * @throws \EasySwoole\Mysqli\Exception\Exception
     * @throws \EasySwoole\ORM\Exception\Exception
     * @throws \Throwable
     */
    public function lists()
    {
        $where = ['is_delete' => 0];
        $table_date = '';//月份分表
        if (UserIdentityHelper::getType() == UserModel::TYPE_INSIDE) {
            /**后台管理员*/
            if ($this->checkField($this->user_id)) {
                $where['user_id'] = $this->user_id;
            }
        } else {
            $this->user_id    = UserIdentityHelper::getId();
            $where['user_id'] = $this->user_id;
        }

        if ($this->checkField($this->id)) {
            $where['id'] = $this->id;
        }
        if ($this->checkField($this->type)) {
            $where['type'] = $this->type;
        }
        if ($this->checkField($this->created_at) && strtotime($this->created_at) > 0) {
            $this->created_at = date('Y-m-d', strtotime($this->created_at));
            $where['created_at'] = [[$this->created_at . ' 00:00:00', $this->created_at . ' 23:59:59'], 'between'];
            $table_date = date('Ym', strtotime($this->created_at));
        }
        if ($this->checkField($this->date) && strtotime($this->date) > 0) {
            $firstDay = date('Y-m-01', strtotime($this->date));
            $lastDay = date('Y-m-t', strtotime($this->date));
            $where['created_date'] = [[$firstDay, $lastDay ], 'between'];
            $table_date = date('Ym', strtotime($this->date));
        }
        if ($this->checkField($this->start_create_at) && $this->checkField($this->end_create_at)) {
            /**查询时间不能跨月*/
            $start_month = date('Y-m', strtotime($this->start_create_at));
            $end_month = date('Y-m', strtotime($this->end_create_at));
            if($start_month != $end_month) {
                throw new DefaultException('查询时间不能跨月');
            }
            $where['created_at'] = [[$this->start_create_at, $this->end_create_at], 'between'];
            $table_date = date('Ym', strtotime($this->start_create_at));
        }elseif ($this->checkField($this->start_create_at)) {
            $where['created_at'] = [$this->start_create_at, '>='];
            $table_date = date('Ym', strtotime($this->start_create_at));
        }elseif ($this->checkField($this->end_create_at)) {
            $where['created_at'] = [$this->end_create_at, '<='];
            $table_date = date('Ym', strtotime($this->end_create_at));
        }
        $model = MemberProfitScoreModel::makeQueryModel($where, []);
        $model = CommonHelper::setTableNameByMonth($model, $table_date);
        $model->field('created_date, sum(score) as total_score, count(1) as total_count');
        $model->group('created_date');
        $model->order('created_date', 'ASC');

        $modelResult = $model->all();

        $list = [];

        foreach ($modelResult as $v) {
            $list[$v->created_date] = [
                'created_date' => $v->created_date,
                'total_score'  => $v->total_score,
                'total_count'  => $v->total_count,
            ];
        }
        if (!empty($list)) {
            $firstDay = current($list)['created_date'];
            $firstDay = date('Y-m-01', strtotime($firstDay));
            $lastDay  = date('Y-m-t', strtotime($firstDay));
            /**
             * 循环遍历$firstDay所在月份的所有日期
             * 从$firstDay开始，直到该月的最后一天（$lastDay）
             * 每次循环增加一天（86400秒）
             */
            $data = [];
            for ($i = strtotime($lastDay); $i >= strtotime($firstDay); $i -= 86400) {
                $day = date('Y-m-d', $i);
                if (isset($list[$day])) {
                    $data[] = $list[$day];
                } else {
                    $data[] = [
                        'created_date' => $day,
                        'total_score'  => 0,
                        'total_count'  => 0,
                    ];
                }
            }
        }

        $pagination = $this->getPagination();
        $pagination['total_count'] = count($data);
        $result = [
            'list'       => $data,
            'pagination' => $pagination //['page' => 1, 'page_size' => 31, 'total_count' => count($data)]
        ];
        return $result;
    }

}
